package com.woniu.microcommunityserver.tribal.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommunityserver.res.ResponseMsg;
import com.woniu.microcommunityserver.tribal.entity.InterestTribalInfoEntity;
import com.woniu.microcommunityserver.tribal.service.InterestTribalInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("interest")
public class InterestTribalInfoController {
    @Resource
    private InterestTribalInfoService interestTribalInfoService;

    @GetMapping("findAll")
    public ResponseMsg findAll(int pageNum){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<InterestTribalInfoEntity> interest = interestTribalInfoService.findAll();
        PageInfo<InterestTribalInfoEntity> pageInfo = new PageInfo<InterestTribalInfoEntity>(interest);
        return ResponseMsg.builder().code(200).msg("所有兴趣部落查询成功！").data(pageInfo).build();
    }

    @GetMapping("findAny")
    public ResponseMsg findAny(int pageNum,InterestTribalInfoEntity interestTribalInfoEntity){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        Map<String,Object> map =new HashMap<>();
        if (interestTribalInfoEntity.getTribalName()==""){
            interestTribalInfoEntity.setTribalName(null);
        }
        map.put("tribalName",interestTribalInfoEntity.getTribalName());
        map.put("fkTribalCategoryId",interestTribalInfoEntity.getFkTribalCategoryId());
        map.put("tribalState",interestTribalInfoEntity.getTribalState());
        List<InterestTribalInfoEntity> interest = interestTribalInfoService.findAny(map);
        PageInfo<InterestTribalInfoEntity> pageInfo = new PageInfo<InterestTribalInfoEntity>(interest);
        return ResponseMsg.builder().code(200).msg("指定部落查询成功！").data(pageInfo).build();
    }

    @PostMapping("insert")
    public ResponseMsg insertInterest(@RequestBody InterestTribalInfoEntity interestTribalInfoEntity){
        boolean flag = interestTribalInfoService.insertInterest(interestTribalInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("新增成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("新增失败！").build();
        }
    }

    @PutMapping("update")
    public ResponseMsg updateInterest(InterestTribalInfoEntity interestTribalInfoEntity){
        boolean flag=interestTribalInfoService.update(interestTribalInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("修改成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("修改失败！").build();
        }
    }

    @GetMapping("updateState")
    public ResponseMsg updateStateById(Integer tribalState, Long id){
        System.out.println(tribalState+id);
        boolean flag=interestTribalInfoService.updateState(tribalState, id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("修改成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("修改失败！").build();
        }
    }


}
